<?php

////密钥
//$secretKey = 'ERWIx22Ym7';
//// 游戏接口地址 
//$gameUrl = 'http://mc2.new/loginapi.php';

// 平台处理登录
$username = $_POST['username'];
if (empty($username)) {
	header('Location:login.html');
	exit;
}
//// ************* 处理验证处理过程......
//
//// 使用已登录账号通知游戏服务器
//$timestamp = time();
//$code = md5(md5($username.md5($secretKey)).md5($timestamp));

//echo 333;

//header('content-type:text/html;charset=utf-8;');
//header('Location:'.$gameUrl.'?op=login&username='.$username.'&timestamp='.$timestamp.'&code='.$code);
//exit;
//echo $gameUrl.'?op=login&username='.$username.'&timestamp='.$timestamp.'&code='.$code;

require '../inc/inc.php';
include 'api/ucenter/config_ucenter.php';
include 'api/ucenter/uc_client/client.php';

list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);

if (!User::isUserExists($username)) {
	//在UCenter注册用户信息
	$uid = uc_user_register($_POST['username'], $_POST['password'], $_POST['email']);
	if($uid <= 0) {
		header('refresh:2;url=http://mc2.new/login.html');
		if($uid == -1) {
			echo '用户名不合法';
		} elseif($uid == -2) {
			echo '包含要允许注册的词语';
		} elseif($uid == -3) {
			echo '用户名已经存在';
		} elseif($uid == -4) {
			echo 'Email 格式有误';
		} elseif($uid == -5) {
			echo 'Email 不允许注册';
		} elseif($uid == -6) {
			echo '该 Email 已经被注册';
		} else {
			echo '未定义';
		}
		exit;
	} else {
		$ret = User::create($username);
		//注册成功，设置 Cookie，加密直接用 authcode 函数，用户使用自己的函数
//		setcookie('auth', authcode($uid."\t".$_POST['username'], 'ENCODE'));
	}
} else {
	$ret = false;
	
	list($uid, $username, $password, $email) = uc_user_login($_POST['username'], $_POST['password']);
	if($uid > 0) {
		//用户登陆成功，设置 Cookie，加密直接用 authcode 函数，用户使用自己的函数
		setcookie('auth', authcode($uid."\t".$username, 'ENCODE'));
		//生成同步登录的代码
		$ret = User::login($username);
	} elseif($uid == -1) {
		echo '用户不存在,或者被删除';
	} elseif($uid == -2) {
		echo '密码错';
	} else {
		echo '未定义';
	}
//	$ret = User::login($username);
}
if ($ret) {
	header('Location:http://mc2.new/client/mc2.html');
}